package orgs.example.controller;

import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import orgs.example.pojo.Result;
import orgs.example.pojo.User;
import orgs.example.service.UserService;
import orgs.example.utils.JwtUtil;
import orgs.example.utils.Md5Util;
import orgs.example.utils.ThreadLocalUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user){

        User u = userService.findByUserAccount(user.getUserAccount());

        if(u==null){
            user.setUserStatus("待审核");
            if(user.getUserRole().equals("员工") || user.getUserRole().equals("管理员")){
                user.setUserCompany("大华农会议室管理（有限）公司");
            }else{
                user.setUserCompany("");
            }
            user.setUserTelephone("");
            userService.register(user);
            return Result.success();
        }else{
            return Result.error("用户名已被占用");
        }

    }

    @PostMapping("/login")
    public Result<Object> login(@RequestBody User user){
        String username = user.getUserAccount();
        String password = user.getUserPassword();

        System.out.println(username);
        System.out.println(password);

        User loginUser = userService.findByUserAccount(username);

        if(loginUser == null) {

            return Result.error("用户名错误");
        }

        String status = loginUser.getUserStatus();
            if (status.equals("待审核")) {
                return Result.error("注册待审核");
            } else if (status.equals("未通过")) {
                return Result.error("注册未通过");
            } else if (status.equals("已冻结")) {
                return Result.error("账号已冻结");
            }

//        if (Md5Util.getMD5String(password).equals(loginUser.getUserPassword())) {
            if(password.equals(loginUser.getUserPassword())){
            Map<String, Object> claims = new HashMap<>();
            claims.put("userId", loginUser.getUserId());

            String token = JwtUtil.genToken(claims);

            Map<String, String> map = new HashMap<>();
            map.put("jwt",token);
            map.put("userRole",loginUser.getUserRole());

            return Result.success("登录成功",map);
        }

        return Result.error("密码错误");

    }

    @GetMapping("/getuserInfo")
    public Result<User> getuserinfo(){

        User user = userService.getuserinfo();

        if(user == null){
            return Result.error("用户信息获取失败");
        }
        return Result.success("用户信息获取成功",user);

    }

    @PatchMapping("/edituserInfo")
    public Result edituserInfo(@RequestBody User user){

        userService.edituserInfo(user);

        return Result.success("个人信息修改成功");
    }

    @PatchMapping("/password")
    public Result editpassword(@RequestBody Map<String,String> params){

        String oldPwd = params.get("oldPwd");
        String newPwd = params.get("newPwd");
        String rePwd = params.get("rePwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少必要的参数");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        User loginUser = userService.findByUserId(userId);
//        if (!loginUser.getUserPassword().equals(Md5Util.getMD5String(oldPwd))){
        if(!loginUser.getUserPassword().equals(oldPwd)){
            return Result.error("原密码填写不正确");
        }

        //newPwd和rePwd是否一样
        if (!rePwd.equals(newPwd)){
            return Result.error("两次填写的新密码不一样");
        }

        userService.updatePwd(newPwd);

        return Result.success("修改密码成功");
    }



}

